﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DiscoveryLogic.Analytics.BusinessLogic.Entities;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.UI.Common.Events;
using DiscoveryLogic.Common.Utilities;
using Microsoft.Practices.Composite.Events;
using Microsoft.Practices.Composite.Wpf.Events;

namespace DiscoveryLogic.Analytics.Modules.Entities
{
    public class DocListPresenter
    {
        private DocListView _View;
		/// <summary></summary>
		public DocListView View
		{
			get
			{
				return this._View;
			}
			set
			{
				this._View = value;
				if(this._View !=null)
				{
					this._View.DocSelected += new DocListView.OnDocSelected(SelectDoc);
				}
			}
		}

		private readonly IEventAggregator _EventAggregator;
        private SubscriptionToken _SubscriptionToken;

        public DocListPresenter(IEventAggregator eventAggregator)
		{
			this._EventAggregator = eventAggregator;
		}

        public void SelectDoc(Document doc)
        {
            if (doc != null)
            {
                this._EventAggregator.GetEvent<DocumentSelectedEvent>().Publish(doc);
            }
        }

        public void RegisterDocSelectionChangedEventHandler(Document doc)
        {
            DocumentSelectedEvent docSelectedEvent =
                    _EventAggregator.GetEvent<DocumentSelectedEvent>();
            if (this._SubscriptionToken != null)
            {
                docSelectedEvent.Unsubscribe(_SubscriptionToken);
            }
            this._SubscriptionToken = docSelectedEvent.Subscribe(
                DocSelectedHandler,
                ThreadOption.UIThread,
                false);
        }

        private void DocSelectedHandler(Document doc)
        {
            this._View.SelectDoc(doc);
        }
    }
}
